package test2;

/**
 * Test2_17
 */
public class Test2_17 {

  public static void main(String[] args) {
    System.out.println(factFor(3));
    System.out.println(factWhile(3));
    System.out.println(factDoWhile(3));
    System.out.println(factRecursion(3));
  }

  /**
   * 递归阶乘
   */
  public static int factRecursion(int n) {
    if (0 == n) {
      return 1;
    }
    n = Math.abs(n);
    return factRecursion(n - 1) * n; 
  }

  /**
   * do-while 阶乘
   */
  public static int factDoWhile(int n) {
    if (0 == n) {
      return 1;
    }
    n = Math.abs(n);
    int count = 1;
    int i = 1;
    do {
      count *= i;
      i++;
    } while (i <= n);
    return count;
  }

  /**
   * while 阶乘
   */
  public static int factWhile(int n) {
    if (0 == n) {
      return 1;
    }
    n = Math.abs(n);
    int count = 1;
    int i = 1;
    while (i <= n) {
      count *= i;
      i++;
    }
    return count;
  }

  /**
   * for 阶乘
   */
  public static int factFor(int n) {
    if (0 == n) {
      return 1;
    }
    n = Math.abs(n);
    int count = 1;
    for (int i = 1; i <= n; i++) {
      count *= i;
    }
    return count;
  }
}